package top.yohann.study.common.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.yohann.study.common.annotion.AuthToken;
import top.yohann.study.common.common.Result;
import top.yohann.study.common.dto.MailDto;
import top.yohann.study.common.service.MailService;

import javax.validation.constraints.Email;

/**
 * @author Yeung Wah
 * @since 2021/11/21
 */
@Api("邮件服务")
@RestController
@RequestMapping("/mail")
@AuthToken
@Validated
public class MailController {

    @Autowired
    private MailService mailService;

    @ApiOperation("发送邮件")
    @PostMapping("send")
    public Result<String> sendMail(@Validated @ApiParam("邮件内容") @RequestBody MailDto mailDto) {
        boolean send = mailService.sendMail(mailDto);
        return Result.state(send);
    }

    @ApiOperation("发送验证码")
    @PostMapping("sendCode")
    public Result<String> sendCode(@ApiParam("邮件内容 {code}替换为验证码") @RequestBody MailDto mailDto) {
        boolean send = mailService.sendCode(mailDto);
        return Result.state(send);
    }

    @ApiOperation("校验验证码")
    @GetMapping("checkCode")
    public Result<String> checkCode(@Email(message = "请正确填写邮箱") @ApiParam("邮箱") @RequestParam String mail, @ApiParam("验证码") @RequestParam String code) {
        boolean checkCode = mailService.checkCode(mail, code);
        return Result.state(checkCode);
    }

}